package com.js.rtu.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 说明：日期格式化工具
 */
public class DateFormatUtils {

    private static final String DATE_FORMAT_PATTERN_YMD = "yyyyMMdd";
    private static final String DATE_FORMAT_PATTERN_YMD_HM = "yyyy-MM-dd";

    /**
     * 时间戳转字符串
     *
     * @param timestamp     时间戳
     * @param isPreciseTime 是否包含时分
     * @return 格式化的日期字符串
     */
    public static String long2Str(long timestamp, boolean isPreciseTime) {
        return long2Str(timestamp, getFormatPattern(isPreciseTime));
    }

    public static String long2Str(long timestamp, String pattern) {
        return new SimpleDateFormat(pattern, Locale.CHINA).format(new Date(timestamp));
    }

    /**
     * 字符串转时间戳
     *
     * @param dateStr       日期字符串
     * @param isPreciseTime 是否包含时分
     * @return 时间戳
     */
    public static long str2Long(String dateStr, boolean isPreciseTime) {
        return str2Long(dateStr, getFormatPattern(isPreciseTime));
    }

    private static long str2Long(String dateStr, String pattern) {
        try {
            return new SimpleDateFormat(pattern, Locale.CHINA).parse(dateStr).getTime();
        } catch (Throwable ignored) {
        }
        return 0;
    }

    private static String getFormatPattern(boolean showSpecificTime) {
        if (showSpecificTime) {
            return DATE_FORMAT_PATTERN_YMD_HM;
        } else {
            return DATE_FORMAT_PATTERN_YMD;
        }
    }


    /**
     * 获得指定日期的后一天
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay){
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day+1);

        String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayAfter;
    }


    /**
     * 获得指定日期的后一年
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedYearAfter(String specifiedDay){
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day=c.get(Calendar.YEAR);
        c.set(Calendar.YEAR,day+1);

        String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayAfter;
    }
    /**
     * 获得指定日期的后一年
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedYearBefore(String specifiedDay){
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day=c.get(Calendar.YEAR);
        c.set(Calendar.YEAR,day-1);

        String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayAfter;
    }


    /**
     * 带时区时间转String
     * @return
     */
    public static String Z2String(String time){
//        String MSG="2021-07-22T14:28:41.257+08:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz", Locale.CHINA);
        Date applyDate = null;
        try {
            applyDate = sdf.parse( time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String stringDate="";
        if (applyDate != null) {
            stringDate = sdf1.format(applyDate);
        }
        return stringDate;
    }

    /**
     * 带时区时间转String
     * @return
     */
    public static String Z2String(String time,String format){
//        String MSG="2021-07-22T14:28:41.257+08:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz", Locale.CHINA);
        Date applyDate = null;
        try {
            applyDate = sdf.parse( time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat sdf1 = new SimpleDateFormat(format, Locale.CHINA);
        String stringDate="";
        if (applyDate != null) {
            stringDate = sdf1.format(applyDate);
        }
        return stringDate;
    }
    /**
     * 带时区时间转String
     * @return
     */
    public static String Z2String(String time,String from,String to){
//        String MSG="2021-07-22T14:28:41.257+08:00";
        SimpleDateFormat sdf = new SimpleDateFormat(from, Locale.CHINA);
        Date applyDate = null;
        try {
            applyDate = sdf.parse( time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat sdf1 = new SimpleDateFormat(to, Locale.CHINA);
        String stringDate="";
        if (applyDate != null) {
            stringDate = sdf1.format(applyDate);
        }
        return stringDate;
    }
}
